/*
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
 ** You may obtain a copy of the License at
 **
 **     http://www.apache.org/licenses/LICENSE-2.0
 **
 ** Unless required by applicable law or agreed to in writing, software
 ** distributed under the License is distributed on an "AS IS" BASIS,
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */

package org.geohunter.activity.edit;

import org.geohunter.Clock;
import org.geohunter.GeoObject;
import org.geohunter.Geocache;
import org.geohunter.Tags;
import org.geohunter.activity.compass.Util;
import org.geohunter.database.CacheWriter;
import org.geohunter.database.DbFrontend;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class EditCacheActivityDelegate {
    public static class CancelButtonOnClickListener implements OnClickListener {
        private final Activity mActivity;
    
        public CancelButtonOnClickListener(Activity activity) {
            mActivity = activity;
        }
    
        public void onClick(View v) {
            mActivity.setResult(Activity.RESULT_CANCELED, null);
            mActivity.finish();
        }
    }

    public static class CacheSaverOnClickListener implements OnClickListener {
        private final Activity mActivity;
        private final EditCacheActivityDelegate mEditCache;
    
        public CacheSaverOnClickListener(Activity activity, 
                EditCacheActivityDelegate editCacheDelegate) {
            mActivity = activity;
            mEditCache = editCacheDelegate;
        }

        public void onClick(View v) {
            mEditCache.saveIfNeeded();
            final Intent i = new Intent();
            mActivity.setResult(Activity.RESULT_OK, i);
            mActivity.finish();
        }
    }

    private final EditText mId;
    private final EditText mLatitude;
    private final EditText mLongitude;
    private final EditText mName;
    private GeoObject mOriginalGeocache;
    private final DbFrontend mDbFrontend;
    
    public EditCacheActivityDelegate(EditText id, 
            EditText name, EditText latitude, EditText longitude,
            DbFrontend dbFrontend) {
        mId = id;
        mName = name;
        mLatitude = latitude;
        mLongitude = longitude;
        mDbFrontend = dbFrontend;
    }

    public void set(GeoObject geoObject) {
        mOriginalGeocache = geoObject;
        mId.setText(geoObject.getId());
        mName.setText(geoObject.getName());
        mLatitude.setText(Util.formatDegreesAsDecimalDegreesString(geoObject.getLatitude()));
        mLongitude.setText(Util.formatDegreesAsDecimalDegreesString(geoObject.getLongitude()));
        //mLatitude.setText(Double.toString(geocache.getLatitude()));
        //mLongitude.setText(Double.toString(geocache.getLongitude()));
    }
    
    protected void saveIfNeeded() {
        String id = mId.getText().toString();
        String name = mName.getText().toString();
        double latitude = Util.parseCoordinate(mLatitude.getText());
        double longitude = Util.parseCoordinate(mLongitude.getText());

        if (id.equals(mOriginalGeocache.getId())
                && name.equals(mOriginalGeocache.getName())
                && Util.approxEquals(latitude, mOriginalGeocache.getLatitude())
                && Util.approxEquals(longitude, mOriginalGeocache.getLongitude())) {
            //Doesn't need to be saved
        } else {
            String updateTime = Clock.getCurrentStringTime();
            CacheWriter cacheWriter = mDbFrontend.getCacheWriter();
            cacheWriter.startTransaction();
            if(mOriginalGeocache instanceof Geocache) {
                cacheWriter.updateCache(id, name, latitude, longitude, updateTime);
            } else {
                cacheWriter.updateWaypoint(id, name, latitude, longitude, updateTime);
            }
            cacheWriter.endTransaction();
            mDbFrontend.setGeocacheTag(id, Tags.LOCKED_FROM_OVERWRITING, true);
        }
        
    }
}